package com.da.javatest.leetCode;

import java.util.Arrays;

/**
 * @author chenlida
 * @date 2019/1/15 10:33
 * @description
 */
public class Code931 {
    public int minFallingPathSum(int[][] A) {
        int l1 = A.length - 1;
        int l2 = A[0].length - 1;

        for (int i = 0; i < l1; i++) {
            if (l2 > 0) {
                for (int j = 0; j <= l2; j++) {
                    if (j == 0) {
                        A[i + 1][j] = A[i + 1][j] + Math.min(A[i][j], A[i][j + 1]);
                    } else if (j == l2) {
                        A[i + 1][j] = A[i + 1][j] + Math.min(A[i][j], A[i][j - 1]);
                    } else {
                        A[i + 1][j] = A[i + 1][j] + Math.min(A[i][j], Math.min(A[i][j - 1], A[i][j + 1]));
                    }
                }
            }
        }
        Arrays.sort(A[l1]);
        return A[l1][0];
    }
}
